<template>
	<view class="main">
		<view class="list_item">
			<image class="item_img" :src="dataList.avatar" mode=""></image>
			<view class="right">
				<view class="right_top">
					<h3>{{ dataList.name }}</h3>
					<span>{{ dataList.server_content }}</span>
				</view>
				<view class="date_time">
					<span class="label">上门日期：</span>
					<span>{{ dataList.day }} {{dataList.time}}</span>
				</view>
				<view class="date_time">
					<span class="label">上门地址：</span>
					<span>{{ dataList.address }}</span>
				</view>
			</view>
		</view>
		<view class="evaluate">
			<view class="star">
				<span>评价服务</span>
				<image v-for="(item, ind) in starData" :key="ind"
					:src="item.type ? '../../../static/test/stary.png' : '../../../static/test/star.png'" mode=""
					@click="setStarNum(item.id)"></image>
			</view>
			<u--textarea v-model="evaluate.evaluateData" :height="100" placeholder="评价下本次服务吧~"></u--textarea>
			<span class="label">上传图片</span>
			<view class="img_all">
				<view class="bg-img" v-for="(item, index) in evaluate.images" :key="index" @tap="viewImage(index)">
					<image style="width: 100%; height: 100%" :src="$wanlshop.oss(item, 90, 90)" mode="aspectFill">
					</image>
					<view class="icon_x" @tap.stop="delImg(index)"><text class="wlIcon-31guanbi"></text></view>
				</view>

				<view class="dashed" @tap="chooseImage"></view>
			</view>
		</view>
		<view class="btn" @click="evaluateHandle">评价</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: {},
				evaluate: {
					evaluateData: '',
					starId: '',
					images: []
				},
				starData: [{
						id: '1',
						type: false
					},
					{
						id: '2',
						type: false
					},
					{
						id: '3',
						type: false
					},
					{
						id: '4',
						type: false
					},
					{
						id: '5',
						type: false
					}
				]
			};
		},
		onLoad(option) {
			this.getData(option.id);
		},
		mounted() {},
		methods: {
			// 点击评价
			async evaluateHandle() {
				let data = {
					order_id: this.dataList.id,
					star: this.evaluate.starId == '' ? 0 : this.evaluate.starId,
					content: this.evaluate.evaluateData,
					images: this.evaluate.images.length > 0 ? this.evaluate.images.join(',') : ''
				};
				if (data.order_id == '' || data.content == '' || data.images == '') {
					this.$wanlshop.msg('请完善表单！');
				}
				console.log(data);
				// return;
				await uni.request({
					url: '/worker/add_ping',
					method: 'POST',
					data,
					success: (res) => {
						this.$wanlshop.msg(res.msg || '请求成功！');
						this.$wanlshop.to(`/pages/user/record/reservation`);
					}
				});
			},
			// 评价星级
			setStarNum(id) {
				this.evaluate.starId = id;
				this.starData.forEach((item, ind) => {
					if (ind + 1 <= id) {
						item.type = true;
					} else {
						item.type = false;
					}
				});
			},
			// 请求数据
			async getData(id) {
				let data = {
					id
				};
				console.log(data);
				// return;
				await uni.request({
					url: '/worker/get_yu_info',
					method: 'POST',
					data,
					success: (res) => {
						if (res.res.code == 1) {
							this.dataList = res.res.data;
						} else {
							this.$wanlshop.msg(res.msg || '请求失败！');
						}
					}
				});
			},
			// 图片预览
			viewImage(key) {
				uni.previewImage({
					urls: this.evaluate.images,
					current: this.evaluate.images[key]
				});
			},
			// 删除图片
			delImg(key) {
				this.evaluate.images.splice(key, 1);
			},
			// 选择图片上传
			chooseImage(index) {
				uni.chooseImage({
					count: 4, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: (res) => {
						uni.request({
							url: '/wanlshop/common/uploadData',
							success: (updata) => {
								for (let i = 0; i < res.tempFilePaths.length; i++) {
									// 读取图片宽高
									uni.getImageInfo({
										src: res.tempFilePaths[i],
										success: (image) => {
											uni.uploadFile({
												url: updata.data.uploadurl,
												filePath: image.path,
												name: 'file',
												formData: updata.data
													.storage == 'local' ?
													null : updata.data
													.multipart,
												success: (data) => {
													this.evaluate.images
														.push(JSON.parse(
																data.data)
															.data.fullurl);
												},
												fail: (error) => {
													this.$wanlshop.msg(JSON
														.parse(error
															.data).msg);
												}
											});
										}
									});
								}
							}
						});
					}
				});
			}
		}
	};
</script>

<style scoped lang="less">
	.main {
		padding: 20rpx 34rpx;

		.list_item {
			display: flex;
			height: 192rpx;
			padding: 24rpx 20rpx;
			background-color: #fff;
			border-radius: 8rpx;
			margin-bottom: 20rpx;

			.item_img {
				border-radius: 10rpx;
				width: 144rpx;
				height: 144rpx;
			}

			.right {
				padding: 10rpx;
				flex: 1;
				display: flex;
				justify-content: space-between;
				flex-direction: column;

				.right_top {
					width: 100%;
					display: flex;
					align-items: center;
					justify-content: space-between;

					h3 {
						margin-right: 23rpx;
					}

					span {
						// width: 112rpx;
						height: 32rpx;
						padding: 0 10rpx;
						font-size: 22rpx;
						color: #fe6600;
						line-height: 32rpx;
						text-align: center;
						// padding: 0rpx 10rpx;
						border-radius: 4rpx;
						background-color: #fff0e5;
					}
				}

				.date_time {
					font-size: 28rpx;

					.label {
						color: #999999;
					}
				}
			}
		}

		.evaluate {
			background-color: #fff;
			border-radius: 24rpx;
			padding: 38rpx 42rpx 40rpx;
			font-size: 28rpx;

			.star {
				margin-bottom: 26rpx;

				image {
					margin-top: 6rpx;
					margin-left: 10rpx;
					width: 28rpx;
					height: 28rpx;
				}
			}

			.label {
				display: block;
				margin: 40rpx 0 24rpx 0;
			}

			.img_all {
				display: flex;
				flex-wrap: wrap;
				gap: 15rpx;

				.bg-img {
					position: relative;
					width: 192rpx;
					height: 192rpx;

					.icon_x {
						position: absolute;
						top: 0;
						right: 0;
						background-color: red;
						padding: 0 6rpx;
					}
				}
			}
		}

		.btn {
			width: 100%;
			height: 88rpx;
			text-align: center;
			line-height: 88rpx;
			border-radius: 44rpx;
			margin-top: 40rpx;
			background-color: #fe6601;
			color: #fff;
		}
	}

	.dashed {
		width: 192rpx;
		height: 192rpx;
		border-radius: 20rpx;
		background: url('@/static/test/img.png');
		background-repeat: no-repeat;
		background-size: cover;
		display: flex;
		align-items: center;
		justify-content: center;
	}
</style>
